package com.dingup.provider.controller.log;

import com.dingup.api.Util.DateUtil;
import com.dingup.api.base.HttpResContent;
import com.dingup.api.model.log.SysRoleUpdateLog;
import com.dingup.api.model.student.Student;
import com.dingup.provider.service.log.RoleUpdateLogService;
import com.suyeer.basic.util.BHttpResUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.util.List;

import static com.dingup.api.base.BaseUtil.saveErrorMsgAndData;
import static com.dingup.api.base.BaseUtil.saveSuccessMsgAndData;

/**
 * Created by wancheng on 2018/8/10.
 */
@RestController()
@RequestMapping("/log")
public class RoleLogController {

    private static Logger logger = LoggerFactory.getLogger(RoleLogController.class);

    @Autowired
    RoleUpdateLogService roleUpdateLogService;

    @RequestMapping(value = "/get_role_update_log_by_roleId.do",method = RequestMethod.POST)
    public void getUpdateLog(Integer roleId,
                              HttpServletRequest request, HttpServletResponse response) {
        HttpResContent<List<SysRoleUpdateLog>> hrc = new HttpResContent<>(request, response);
        hrc.setFuncDes("根据ID获取身份的修改日志");
        try {
            List<SysRoleUpdateLog> logs = roleUpdateLogService.getUpdateLog(roleId);
            logs.stream().forEach(
                    l->{
                        String date = DateUtil.date2Str(l.getCreateTime());
                        l.setCreateTimeString(date);
                    }
            );
            saveSuccessMsgAndData(hrc, logs);
        } catch (Exception e) {
            saveErrorMsgAndData(hrc, e);
        } finally {
            BHttpResUtil.writeResult(hrc, true);
        }
    }

    @RequestMapping(value = "/get_role_delete_log.do",method = RequestMethod.POST)
    public void getDeleteLog(HttpServletRequest request, HttpServletResponse response) {
        HttpResContent<List<SysRoleUpdateLog>> hrc = new HttpResContent<>(request, response);
        hrc.setFuncDes("获取日志的删除的信息");
        try {
            List<SysRoleUpdateLog> logs = roleUpdateLogService.getDeleteLog();
            logs.stream().forEach(
                    l->{
                        String date = DateUtil.date2Str(l.getCreateTime());
                        l.setCreateTimeString(date);
                    }
            );
            saveSuccessMsgAndData(hrc, logs);
        } catch (Exception e) {
            saveErrorMsgAndData(hrc, e);
        } finally {
            BHttpResUtil.writeResult(hrc, true);
        }
    }
}
